package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.model.vo.IndexCategoryVo;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.service.CategoryService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 对接后台管理系统，/admin/product相关请求
 */
@Slf4j
@RestController
@RequestMapping("/admin/product")
public class CategroyAdminController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @GetMapping("/getAllCategorys")
    public List<IndexCategoryVo> getAllCategoryForIndexHtml(){
        return categoryService.getAllCategoryForIndexHtml();
    }



    /**
     * 获取一级分类列表
     * @return
     */
    @GetMapping("/getCategory1")
    public Result<List<BaseCategory1>> getCategory1(){

        List<BaseCategory1> category1s = categoryService.getCategory1();

        return Result.ok(category1s);
    }

    @GetMapping("/getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2(@PathVariable("category1Id")Long category1Id){

        if (category1Id > 0){
            List<BaseCategory2> category2s = categoryService.getCategory2(category1Id);
            return Result.ok(category2s);
        }else {
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }
    }

    @GetMapping("/getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3(@PathVariable("category2Id")Long category2Id){
        if (category2Id > 0){
            List<BaseCategory3> category3s = categoryService.getCategory3(category2Id);
            return Result.ok(category3s);
        }else{
            return Result.build(null, ResultCodeEnum.SECKILL_ILLEGAL);
        }
    }

    /**
     * 保存平台属性和值
     *
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        log.info("前端提交的数据：{}",baseAttrInfo);

        baseAttrValueService.saveOrUpdateAttrInfoAndValue(baseAttrInfo);

        return Result.ok();
    }

    @GetMapping("getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId")Long attrId){

        List<BaseAttrValue> baseAttrValueList = baseAttrValueService.getAttrValueList(attrId);
        return Result.ok(baseAttrValueList);
    }

}
